#!/usr/bin/perl

$ihex = "main.ihex";
$tosboot = "tosboot.ihex";
$info = `motelist -usb > motelist.txt`;
$path = "build/telosb/";

open list, "<motelist.txt" or die "cannot open file";


sub findid() {
	my ($usbport) = @_;
	for (my $i=0; $i<100; $i++) {
		if ($usbmaps[$i] eq $usbport) {
			return $i;
		}
	}	
	return 0xffff;
     }

for ( $i=0; $i<100; $i++) {

 while (<list>) {

  chomp;
  @ret = split;
  if ($ret[4] =~ /ttyUSB/) {
    $port = $ret[4];
    
    $id=$i;
	
    ##$id = &findid($usbport);
    #print "$_\n";
	
       
     $cmd = "tos-set-symbols --objcopy msp430-objcopy --objdump msp430-objdump --target ihex $path$ihex $path$ihex-$id.out TOS_NODE_ID=$id ActiveMessageAddressC__addr=$id";
     print "$cmd\n";
     system("$cmd");

     $cmd = "tos-bsl --telosb -c $port -r -e -I -p $path$ihex-$id.out";
     print "$cmd\n";
     system("$cmd ");
     print "complete $i\n";
     last;
     }    
  }
   
 }


close list;


